package com.meilai.project.controller.mini;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.dto.business.sale.BillSaleCheckDTO;
import com.meilai.project.dto.business.sale.BillSaleDTO;
import com.meilai.project.dto.business.sale.BillSaleListDTO;
import com.meilai.project.dto.workflow.WorkFlowRomoveDTO;
import com.meilai.project.entity.business.saleOrder.BillSaleAppendix;
import com.meilai.project.entity.media.MediaType;
import com.meilai.project.service.business.intentionScheme.IntentionSchemeService;
import com.meilai.project.service.business.sale.BillSaleService;
import com.meilai.project.util.ThreadLocalUserUtil;
import com.meilai.project.vo.business.intentionScheme.IntentionSchemeVO;
import com.meilai.project.vo.business.sale.BillSaleVO;
import com.meilai.project.vo.business.sale.BillSaleVO2;
import com.meilai.project.vo.business.sale.MediaSpecTypeCountAmountVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xchen
 * @since 2022-03-19
 */
@Api(tags = "小程序-销售单")
@RequestMapping("api/mini/bill_sale")
@RestController
@Validated
@Slf4j
public class MiniBillSaleController {

    @Autowired
    private BillSaleService service;
    @Autowired
    private IntentionSchemeService intentionSchemeService;

    @GetMapping("media_type_count_amount")
    @ApiOperation("每种媒体类型对应的数量及金额")
    public ApiResult<List<MediaSpecTypeCountAmountVO>> mediaTypeCountAmount(
            @ApiParam("dateRangeId") @RequestParam("dateRangeId") Long dateRangeId
    ) {
        try {
            return ApiResult.ok(service.mediaTypeCountAmount(dateRangeId));
        } catch (Exception e) {
            log.error("", e);
            return ApiResult.err("查询错误，请联系管理员！");
        }
    }

    @GetMapping("list-for-market-by-contract-id")
    @ApiOperation(value = "根据合同id查销售单(For市场单)")
    public ApiResult<List<BillSaleVO>> listForMarketByContractId(
            @Param("contractId") Long contractId,
            @Param("mustIncludeBillSaleId") Long mustIncludeBillSaleId,
            @Param("type") Integer type
    ) {
        List<BillSaleVO> BillSaleVOPage = service.listForMarketByContractId(contractId, mustIncludeBillSaleId, type);
        return ApiResult.ok(BillSaleVOPage);
    }

    @GetMapping("list")
    @ApiOperation(value = "列表分页")
    public ApiResult<Page<BillSaleVO>> list(@ApiParam("form") BillSaleListDTO req) {
        if (req.isManager()) {
            req.setUserId(null);
        } else {
            req.setUserId(ThreadLocalUserUtil.get().getId());
        }
        Page<BillSaleVO> BillSaleVOPage = service.selectPageList(req);
        return ApiResult.ok(BillSaleVOPage);
    }

    @GetMapping("detail")
    @ApiOperation(value = "单据明细")
    public ApiResult<BillSaleVO> detail(
            @ApiParam("id") @RequestParam(value = "id", required = false) Long id) {
        return ApiResult.ok(service.getOneById(id));
    }

    @PostMapping("save")
    @ApiOperation("保存")
    public ApiResult<Boolean> save(@ApiParam("json") @RequestBody BillSaleDTO req) {
        try {
            if (service.save(req)) {
                return ApiResult.ok("新增成功");
            } else {
                return ApiResult.err("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("begin")
    @ApiOperation("提交")
    public ApiResult<Boolean> begin(@ApiParam("json") @RequestBody BillSaleDTO req) {
        try {
            if (service.begin(req)) {
                return ApiResult.ok("提交成功");
            } else {
                return ApiResult.err("提交失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @GetMapping("list-by-related")
    @ApiOperation("查询关联的锁位列表（合同或者销售单）")
    public ApiResult<List<IntentionSchemeVO>> listByRelated(
            Long related_id, Integer related_type, Long must_include_id, Integer type) {
        List<IntentionSchemeVO> intentionSchemeVOS = intentionSchemeService.listByRelated(related_id, related_type, must_include_id, type);
        return ApiResult.ok(intentionSchemeVOS);
    }

    @PutMapping("pass")
    @ApiOperation("通过")
    public ApiResult<Boolean> pass(@ApiParam("json") @RequestBody BillSaleCheckDTO req) {
        try {
            if (service.pass(req)) {
                return ApiResult.ok("通过成功");
            } else {
                return ApiResult.err("通过失败");
            }
        } catch (Exception e) {
            log.error("exception", e);
            return ApiResult.err(e.getMessage());
        }
    }

    @PutMapping("return_back")
    @ApiOperation("驳回")
    public ApiResult<Boolean> returnBack(@ApiParam("json") @RequestBody BillSaleCheckDTO req) {
        try {
            if (service.returnBack(req)) {
                return ApiResult.ok("驳回成功");
            } else {
                return ApiResult.err("驳回失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "逻辑删除")
    public ApiResult<Boolean> delete(@RequestBody WorkFlowRomoveDTO dto) {
        try {
            if (service.delete(dto.getId())) {
                return ApiResult.ok("删除成功");
            } else {
                return ApiResult.err("删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }

    @DeleteMapping("actual_delete")
    @ApiOperation(value = "物理删除")
    public ApiResult<Boolean> actualDelete(@RequestBody WorkFlowRomoveDTO dto) {
        try {
            if (service.actualDelete(dto.getId())) {
                return ApiResult.ok("彻底删除成功");
            } else {
                return ApiResult.err("彻底删除失败");
            }
        } catch (Exception e) {
            return ApiResult.err(e.getMessage());
        }
    }


    @GetMapping("dispatch/list")
    @ApiOperation(value = "列表分页 提供给调度查看")
    public ApiResult<Page<BillSaleVO2>> dispatchList(@ApiParam("form") BillSaleListDTO req) {
        Page<BillSaleVO2> BillSaleVOPage = service.dispatchList(req);
        return ApiResult.ok(BillSaleVOPage);
    }


    @PostMapping("confirmMonitor")
    @ApiOperation(value = "确认监播发送")
    public ApiResult<Boolean> confirmMonitor(@RequestParam("id") Long id) {
        return ApiResult.ok(service.confirmMonitor(id));
    }

    @ApiModelProperty("保存附件")
    @PostMapping("uploadAppendix")
    public ApiResult<Boolean> uploadAppendix(@RequestBody List<BillSaleAppendix> appendixList) {
        return ApiResult.ok(service.uploadAppendix(appendixList));
    }

    @ApiModelProperty("根据销售单ID获取路径")
    @GetMapping("getAppendixListById")
    public ApiResult<BillSaleAppendix> getAppendixListById(@RequestParam("id") Long id) {
        return ApiResult.ok(service.getAppendixListById(id));
    }

    @GetMapping("/getMediaTypeArrBySaleId")
    @ApiOperation("根据销售单id获取点位类型数据")
    public ApiResult<List<MediaType>> getMediaTypeArrBySaleId(@RequestParam("id") Long id) {
        return ApiResult.ok(service.getMediaTypeArrBySaleId(id));
    }
}
